Technical Q&As
IC 03 - Sharing Globals Between Apps & Code Fragment Manager (1-May-95)
Q I have a shared library (PowerPC native CFM) that is used by several
applications. The library includes a few global variables that are used in all
of the applications (they declare them as "extern"s). My problem is that each
application seems to have its own copy of the globals, but when I check the
address of the same global in several applications, they are different. Aren't
globals supposed to be sharable between applications?
A The Code Fragment Manager allows shared libraries to allocate and use global
variables in two different ways: Global instantiation or Per-context
instantiation. At link time, you determine whether your library will support
either global or per-context instantiation. The default PPCLink option is to
support per-context instantiation, which is probably why you are seeing
different addresses for the same global across applications.
These are the differences between the two types of instantiation:
a) Global instantiation: CFM allocates a single copy of the library's static
data, regardless of how many clients use the data.
b) Per-context instantiation: CFM allocates one copy of the static data for
each separate process that uses that data.
If you want the CFM to allocate only a single copy of your library's static
data, and if you are using PPCLink 1.2a1 from ETO 16, your link line must
include the option (-share global), which sets the sharing mode for the static
data to global instantiation. If you are using version 1.0 of the PPCLink tool,
you must specify the global instantiation option (-s 4) on your MakePef command
line. For MakePef, the default option is per-context (-s 1).
To learn more about how the Code Fragment Manager deals with global data by
reading pages 1-50 through 1-52 of Inside Macintosh:PowerPC System
Software. "Building for PowerPC" on ETO 16 has additional information on
building a shared library.
Technical Q&As
Previous Question | Contents |
Next Question